<template>
  <a-card :bordered="false" style="height: 90%">
    <div class="table-operator">
      <a-button v-if="$auth('sys_menu_add')" type="primary" icon="plus" @click="openEditDialog((record) => record)">
        新建
      </a-button>
    </div>

    <s-table
      ref="table"
      size="middle"
      :row-key="record => record.nodeId"
      :columns="columns"
      :data="loadData"
      :show-pagination="false"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <div>
            <a-button v-if="$auth('sys_menu_add')" type="primary" style="margin: 10px 10px 0 0" icon="plus" @click="newEditDialog(record)">新增
            </a-button>
            <a-button v-if="$auth('sys_menu_edit')" type="primary" style="margin-right: 10px" @click="openEditDialog(record)">编辑</a-button>
            <a-button v-if="$auth('sys_menu_del')" type="danger" @click="deleteDict(record.nodeId)">删除</a-button>
          </div>
        </template>
      </span>
    </s-table>

    <menu-edit ref="menuEdit" @close="handleOk" />
  </a-card>
</template>
<style scoped lang="scss">
  .table-operator {
    margin-bottom: 10px;
  }
</style>
<script>
import MenuEdit from '@/views/sys/menu/Edit'
import {STable} from '@/components'
import {listMenuTree, deleteMenu} from '@/api/menu'

const menuType = {
  MENU: '菜单',
  BUTTON: '按钮'
}

export default {
  name: 'Index',
  components: {
    STable,
    MenuEdit
  },
  data() {
    return {
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '名称',
          dataIndex: 'node.menuName',
          key: 'node.menuName',
          width: '150px'
        },
        {
          title: '图标',
          dataIndex: 'node.menuIcon',
          key: 'node.menuIcon',
          width: '150px'
        },
        {
          title: '访问地址',
          dataIndex: 'node.href',
          key: 'node.href',
          width: '150px'
        },
        {
          title: '权限',
          dataIndex: 'node.permission',
          key: 'node.permission',
          width: '150px'
        },
        {
          title: '类型',
          dataIndex: 'node.menuType',
          key: 'node.menuType',
          width: '150px',
          customRender: text => menuType[text]

        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '350px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return listMenuTree(Object.assign(parameter, this.queryParam))
          .then(res => {
            res.data.content = this.deleteEmptyChildren(res.data)
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      optionAlertShow: false
    }
  },
  created() {
  },
  methods: {
    deleteDict(id) {
      const _this = this
      _this.$confirm({
        title: '确认删除该字典吗?',
        content: '删除后将不可恢复',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          return deleteMenu(id).then(res => {
            if (res.code === '0') {
              _this.$message.success('删除成功')
            } else {
              _this.$message.error(res.message)
            }
            _this.handleOk()
          })
        },
        onCancel() {
        }
      })
    },
    openEditDialog(obj) {
      var form = Object.assign({}, obj.node)
      this.$refs.menuEdit.initData(form)
    },
    newEditDialog(obj) {
      var form = {parentId: obj.node.id}
      this.$refs.menuEdit.initData(form)
    },
    deleteEmptyChildren(data) {
      if (data.length > 0) {
        for (const i in data) {
          if (data[i].children && data[i].children.length) {
            this.deleteEmptyChildren(data[i].children)
          } else {
            delete data[i].children
          }
        }
      }
      return data
    },
    handleOk() {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
